package cn.gov.pbc.gd.credit.provider.api.online.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.liu.jbase.PageListVo;
import com.liu.jbase.service.mongo.IMongoClient;
import com.liu.tools.DateUtil;

import cn.gov.pbc.gd.credit.api.online.IOnlineUserApi;
import cn.gov.pbc.gd.credit.vo.online.OnlineUserVo;

@Service
public class OnlineUserApiImpl implements IOnlineUserApi {

	@Autowired
	private IMongoClient<OnlineUserVo> mongoClient;
	
	@Override
	public void insert(OnlineUserVo entity) throws Exception {
		mongoClient.insert(entity);
	}

	@Override
	public void updateById(Serializable id, String key, String value) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(OnlineUserVo vo) throws Exception {
		
		Query query = new Query();
		query.addCriteria(Criteria.where("sessionId").is(vo.getSessionId()));
		Update update = new Update();
		if(vo.getLastActiveTime() != null){
			update.set("lastActiveTime", vo.getLastActiveTime());
		}
		
		mongoClient.update(query, update, OnlineUserVo.class);	
	}

	@Override
	public void deleteByQuery(OnlineUserVo vo) throws Exception {
		
		Query query = new Query();
		
		mongoClient.deleteByQuery(query, OnlineUserVo.class);

	}

	@Override
	public OnlineUserVo getOneById(Serializable id) throws Exception {
		
		return mongoClient.getOneById(id, OnlineUserVo.class);
	}

	@Override
	public OnlineUserVo getOne(OnlineUserVo vo) throws Exception {

		Query query = new Query();
		
		return mongoClient.getOne(query, OnlineUserVo.class);
	}

	@Override
	public List<OnlineUserVo> getAll(Class<OnlineUserVo> entity) throws Exception {
		return mongoClient.getAll(entity);
	}

	@Override
	public List<OnlineUserVo> getAll(OnlineUserVo vo) throws Exception {

		Query query = new Query();
		
		return mongoClient.getAll(query, OnlineUserVo.class);
	}

	@Override
	public long getCount(OnlineUserVo vo) throws Exception {
		
		Query query = new Query();
		
		return mongoClient.getCount(query, OnlineUserVo.class);
	}

	@Override
	public PageListVo<OnlineUserVo> getPage(OnlineUserVo vo, PageListVo<OnlineUserVo> page) throws Exception {
		
		Query query = new Query();
		
		if (vo.getLastActiveTime() != null) {

			query.addCriteria(Criteria.where("lastActiveTime").gte(vo.getLastActiveTime()).lte(DateUtil.getCurrentFormatMill()));
		}
		
		query.with(new Sort(new Order(Direction.DESC,"lastActiveTime")));  
		
		return mongoClient.getPage(query, page, OnlineUserVo.class);
	}

	@Override
	public OnlineUserVo findAndUpdate(OnlineUserVo vo) throws Exception {
		
		Query query = new Query();
		query.addCriteria(Criteria.where("sessionId").is(vo.getSessionId()));
		Update update = new Update();
		if(vo.getLastActiveTime() != null){
			update.set("lastActiveTime", vo.getLastActiveTime());
		}
		
		return mongoClient.findAndUpdate(query, update, OnlineUserVo.class);
	}

}
